package com.open.party.cloud.view.home.ziShenJiaoYu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import cn.android.x.model.LeaderUserBean;
import com.open.party.cloud.R;
import com.sinothk.android.utils.XUtils;

import java.util.ArrayList;
import java.util.List;

public class LeaderListAdapter extends RecyclerView.Adapter<LeaderListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<LeaderUserBean> mData;
    private Context mContext;

    public LeaderListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setData(List<LeaderUserBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<LeaderUserBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.leader_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final LeaderUserBean itemData = mData.get(position);

        holder.nameTv.setText(XUtils.string().getNotNullValue(itemData.getLeaderName()));
        holder.mainPostTv.setText(XUtils.string().getNotNullValue(itemData.getMainPost()));
        holder.jobTitleTv.setText(XUtils.string().getNotNullValue(itemData.getPostExt()));

        Glide.with(mContext).load(XUtils.string().getNotNullValue(itemData.getImgUrl()))
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .into(holder.imageView);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout rootView;
        TextView nameTv, mainPostTv, jobTitleTv;
        ImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            nameTv = itemView.findViewById(R.id.nameTv);
            mainPostTv = itemView.findViewById(R.id.mainPostTv);
            jobTitleTv = itemView.findViewById(R.id.jobTitleTv);

            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}